1
|
|
|
/* eslint-disable node/no-deprecated-api */ |
2
|
|
|
var buffer = require('buffer') |
3
|
|
|
var Buffer = buffer.Buffer |
4
|
|
|
|
5
|
|
|
// alternative to using Object.keys for old browsers |
6
|
|
|
function copyProps (src, dst) { |
7
|
|
|
for (var key in src) { |
|
|
|
|
8
|
|
|
dst[key] = src[key] |
9
|
|
|
} |
10
|
|
|
} |
11
|
|
|
if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { |
12
|
|
|
module.exports = buffer |
13
|
|
|
} else { |
14
|
|
|
// Copy properties from require('buffer') |
15
|
|
|
copyProps(buffer, exports) |
16
|
|
|
exports.Buffer = SafeBuffer |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
function SafeBuffer (arg, encodingOrOffset, length) { |
20
|
|
|
return Buffer(arg, encodingOrOffset, length) |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// Copy static methods from Buffer |
24
|
|
|
copyProps(Buffer, SafeBuffer) |
25
|
|
|
|
26
|
|
|
SafeBuffer.from = function (arg, encodingOrOffset, length) { |
27
|
|
|
if (typeof arg === 'number') { |
28
|
|
|
throw new TypeError('Argument must not be a number') |
29
|
|
|
} |
30
|
|
|
return Buffer(arg, encodingOrOffset, length) |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
SafeBuffer.alloc = function (size, fill, encoding) { |
34
|
|
|
if (typeof size !== 'number') { |
35
|
|
|
throw new TypeError('Argument must be a number') |
36
|
|
|
} |
37
|
|
|
var buf = Buffer(size) |
38
|
|
|
if (fill !== undefined) { |
39
|
|
|
if (typeof encoding === 'string') { |
40
|
|
|
buf.fill(fill, encoding) |
41
|
|
|
} else { |
42
|
|
|
buf.fill(fill) |
43
|
|
|
} |
44
|
|
|
} else { |
45
|
|
|
buf.fill(0) |
46
|
|
|
} |
47
|
|
|
return buf |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
SafeBuffer.allocUnsafe = function (size) { |
51
|
|
|
if (typeof size !== 'number') { |
52
|
|
|
throw new TypeError('Argument must be a number') |
53
|
|
|
} |
54
|
|
|
return Buffer(size) |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
SafeBuffer.allocUnsafeSlow = function (size) { |
58
|
|
|
if (typeof size !== 'number') { |
59
|
|
|
throw new TypeError('Argument must be a number') |
60
|
|
|
} |
61
|
|
|
return buffer.SlowBuffer(size) |
62
|
|
|
} |
63
|
|
|
|
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: